/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rd.project.controller;

import com.rd.project.service.PuntenService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;

@ManagedBean(name = "uploadController")
@RequestScoped
public class controller {

    @EJB
    private PuntenService PuntenService;
    
    private UploadedFile uploadedFile;

    //private String path;
    //private String pad;
    //private String destination = "D:\\tmp\\";
    
    

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public String excelOpslaan() throws IOException {
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        
        String basePath = "C:" + File.separator + "temp" + File.separator;
        File outputFilePath = new File(basePath + fileName);
        
        InputStream inputStream = null;
        OutputStream outputStream = null;
        
        try {
			inputStream = uploadedFile.getInputStream();
			outputStream = new FileOutputStream(outputFilePath);
 
			int read = 0;
			final byte[] bytes = new byte[1024];
			while ((read = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, read);
			}
 
		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			if (outputStream != null) {
				outputStream.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
        PuntenService.getExcel(basePath + fileName);

        
        return "index";
    }
    
    //PuntenService.getExcel(path);

    

    //@PostConstruct
    //public void init() {
    //   excel = PuntenService.getExcel(path);
    //}
    //public List<String> getExcel(String path) {
    //    return PuntenService.getExcel(path);
    //}

}
